from django.db import models

# related_name  在自关联模型中外键必须加此选项把 一的这方隐式生成的 多的一方模型史小写_set字段改个别名
class Area(models.Model):
    """省市区"""
    name = models.CharField(max_length=20, verbose_name='名称')
    # 自关联的表,外键指向当前模型本身
    parent = models.ForeignKey('self', on_delete=models.SET_NULL, related_name='subs', null=True, blank=True, verbose_name='上级行政区划')

    class Meta:
        db_table = 'tb_areas'
        verbose_name = '省市区'
        verbose_name_plural = '省市区'

    def __str__(self):
        return self.name



# Area.objects.filter(parent=None)  # 查询所有省
#
# hb = Area.objects.get(id=130000)
# hb.subs.all()  # 获取指定省下面的所有市

# Area.objects.filter(parent_id=130000)

# sjz = Area.objects.get(id=130100)
# sjz.subs.all()

# Area.objects.filter(parent_id=130100)

# GDArea.area_set.all()
# ZSArea.parent
#
# class BookInfo(models.Model):
#     btitle = 'xxx'
#     heroinfo_set
#     heros
#     hero = 'xxx'
#
# class HeroInfo(models.Model):
#     hname = 'xxx'
#     hbook = 'xxxx  related_name='heros''
#
#
# # hero.hbook
# book.heroinfo_set.all()
# book.heros.all()

# id      name     prent
# 130000  广东省    null
# 130001  深圳市   130000
# 1300011 宝安区   130001